#include "stdafx.h"
#include "RenderSystem.h"
#include "EntityDataStore.h"
#include "TransformComponent.h"
#include "SpriteComponent.h"
#include "EntityManager.h"
#include "AnimatedComponent.h"
#include "ResourceManager.h"
#include "Font.h"
#include "GameEngine.h"

using namespace std;

namespace ZeEngine
{
    RenderSystem::RenderSystem() : IComponentSystem()
    {
        AddRequiredComponent("TransformComponent");
        AddNeedAtLeastOneComponent("SpriteComponent");
        AddNeedAtLeastOneComponent("AnimatedComponent");
    }

    RenderSystem::~RenderSystem()
    {
    }

	bool RenderSystem::Init(GameEngine& gameEngine)
	{
        return m_renderer.Init(gameEngine.GetConfiguration().GetLogicalResolution());
	}

    void RenderSystem::Render(SDL_Renderer& renderer, GameEngine& gameEngine, float interpolation)
    {       
        EntityManager& entityManager = gameEngine.GetEntityManager();

        auto stores = entityManager.GetAllEntities();

        for (auto pEntity : stores)
        {
            if (pEntity && IsEntitySupported(pEntity))
            {
                auto pTransformComponent = pEntity->GetComponent<TransformComponent>();
                auto pSpriteComponent = pEntity->GetComponent<SpriteComponent>();
                auto pAnimatedComponent = pEntity->GetComponent<AnimatedComponent>();

                if (pTransformComponent)
                {
                    if (pSpriteComponent)
                    {
                        m_renderer.Render(renderer, *pTransformComponent, *pSpriteComponent, interpolation);
                    }
                    else if (pAnimatedComponent)
                    {
                        m_renderer.Render(renderer, *pTransformComponent, *pAnimatedComponent, interpolation);
                    }
                }
            }
        }   

        RenderImpl(renderer);
    }

	void RenderSystem::RenderImpl(SDL_Renderer& renderer)
	{
        m_renderer.RenderImpl(renderer);
	}
}
